<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/22 0022
 * Time: 11:39
 */

namespace app\wap\controller;


use think\Db;

class Login extends Base
{
    //todo 注册
    public function Register(){
        $map = ['phone'=>$this->data['phone']];
        if(Db::name('user')->where($map)->count())return $this->returnError('该号码已经被注册');
        $key = $this->data['phone'].'reg';
        $yzm = $this->redis->get($key);
        if(!$yzm || $yzm != $this->data['yzm'])return $this->returnError('验证码错误');
        $pid = isset($this->data['pid']) ? $this->data['pid']  : false;
        $set = [
            'phone'     =>$this->data['phone'],
            'password'  =>md5($this->data['password']),
            'invitation'=>$this->GetRandomStr(10),
            'rt'        =>1,
            'create_time'=>time(),
            'headimg'   => Db::name('common')->where(['type'=>98])->value('content'),
            'nickname'  =>$this->GetRandomStr(5).rand(100,999),
        ];
        if($pid){
            $pu = Db::name('user')->where(['invitation'=>$pid])->value('uid');
            if($pu)$set['pid'] = $pu;
        }
        Db::startTrans();
        try{
            $uid = Db::name('user')->insertGetId($set);
            Db::name('user_service')->insert(['uid'=>$uid]);
            Db::name('sign')->insert(['uid'=>$uid]);
            Db::name('user_msg')->insert(['uid'=>$uid]);
            Db::commit();
            $this->redis->delete($key);
            return $this->returnSuccess(null,'注册成功');
        }catch (\Exception $e){
            Db::rollback();
            return $this->returnError($this->Error);
        }
    }
    //todo 注册验证码
    public function RegYzm(){return $this->GetYzm($this->data['phone'],'reg');}

    //todo 普通登录
    public function Login(){
        $key = config('LOGINKEY');
        $map =['phone'=>$this->data['phone'],'password'=>md5($this->data['password']),'rt'=>1];
        $user = Db::name('user')->where($map)->find();
        if(!$user)return $this->returnError('手机号或密码不正确');
      	$auth = ['uid'=>$user['uid'],'expire'=>time(),'code'=>$user['invitation'],'reg_id'=>$this->data['reg_id'],'rand'=>rand(1000,9999)];
        $token = $this->JWT_encryption($key,$auth);
        $set = [
            'uid'       =>$user['uid'],
            'log_time'  =>time(),
            'reg_id'    =>$this->data['reg_id'],
          	'token'		=>$token
        ];
        Db::name('user')->update($set);
        return $this->returnSuccess($token);
    }

    //todo 忘记密码
    public function Forget(){
        $map = ['phone'=>$this->data['phone']];
        $uid = Db::name('user')->where($map)->value('uid');
        if(!$uid)return $this->returnError('手机号不存在');
        $key = $this->data['phone'].'forget';
        $yzm = $this->redis->get($key);
        if(!$yzm || $yzm != $this->data['yzm'])return $this->returnError('验证码错误');
        $set = [
            'uid'       =>$uid,
            'password'  =>md5($this->data['password']),
            'update_time'=>time(),
        ];
        $res = Db::name('user')->update($set);
        if($res){
            $this->redis->delete($key);
            return $this->returnSuccess(null,'修改成功');
        }else{
            return $this->returnError('网络错误');
        }
    }

    //todo 忘记密码验证码
    public function ForgetYzm(){return $this->GetYzm($this->data['phone'],'forget');}

    //todo 微信登录
    public function WxLogin(){
        $key = config('LOGINKEY');
        $map =['openid'=>$this->data['openid'],'rt'=>2];
        $uid = Db::name('user')->where($map)->value('uid');
        if(!$uid){
            $set = [
                'reg_id'    =>$this->data['reg_id'],
                'invitation'=>$this->GetRandomStr(10),
                'rt'        =>2,
                'create_time'=>time(),
                'log_time'  =>time(),
                'openid'    =>$this->data['openid'],
                'headimg'   => Db::name('common')->where(['type'=>98])->value('content'),
                'nickname'  =>$this->GetRandomStr(5).rand(100,999),
            ];
            $uid = Db::name('user')->insertGetId($set);
            Db::name('user_service')->insert(['uid'=>$uid]);
            Db::name('sign')->insert(['uid'=>$uid]);
            Db::name('user_msg')->insert(['uid'=>$uid]);
        }
        $set = [
            'uid'       =>$uid,
            'log_time'  =>time(),
            'reg_id'    =>$this->data['reg_id'],
        ];
        Db::name('user')->update($set);
        $user = Db::name('user')->where(['uid'=>$uid])->find();
        $auth = ['uid'=>$uid,'expire'=>time()+86400,'code'=>$user['invitation'],'reg_id'=>$user['reg_id']];
        $token = $this->JWT_encryption($key,$auth);
        return $this->returnSuccess($token);
    }
  
  	public function LoginOut(){
        $set = [
            'uid'   =>$this->uid,
            'reg_id'=>null,
        ];
        $res = Db::name('user')->update($set);
        if($res)return $this->returnSuccess(null,'注销成功');
        return $this->returnError($this->Error);
    }
  
  
  	

}